package com.wyf.springbootmybatis.controller;

import com.github.pagehelper.PageInfo;
import com.wyf.springbootmybatis.exceptions.ParamsException;
import com.wyf.springbootmybatis.po.User;
import com.wyf.springbootmybatis.query.UserQuery;
import com.wyf.springbootmybatis.service.impl.UserServiceImpl;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
// @Controller
@Api(tags = "用户管理模块")
public class UserController {

    @Resource
    private UserServiceImpl userService;

    /**
     * 通过用户名查询用户对象
     * @param userName
     * @return
     */
    @GetMapping("/user/name/{userName}")
    @ApiOperation(value = "根据用户名查询用户")
    @ApiImplicitParam(name = "userName", value = "用户名", readOnly = true, paramType = "path")
    public User queryUserByName(@PathVariable String userName) {
        return userService.queryUserByName(userName);
    }

    /**
     * 通过id查询用户
     * @param userId
     * @return
     */
    @GetMapping("/user/{userId}")
    @ApiOperation(value = "根据用户id查询用户")
    @ApiImplicitParam(name = "userId", value = "用户id", readOnly = true, paramType = "path")
    public User queryUserByName(@PathVariable Integer userId) {
        return userService.queryUserById(userId);
    }


    /**
     * 添加用户
     * @return
     */
    @PostMapping("/user")
    @ApiOperation(value = "增加用户")
    @ApiImplicitParam(name = "user", value = "用户实体类", readOnly = true, dataType = "User", paramType = "body")
    public Map<String, Object> addUser(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        // 通过try-catch 捕获service层抛出的异常
        // try {
        //     // 调用Service层的方法，返回Map对象
            userService.addUser(user);
            map.put("code", 200);
            map.put("msg", "添加成功");
        // } catch (ParamsException p) {
        //     map.put("code", p.getCode());
        //     map.put("msg", p.getMsg());
        // } catch (Exception e) {
        //     map.put("code", 500);
        //     map.put("msg", "添加用户失败");
        // }
        return map;
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping("/user")
    @ApiOperation(value = "修改用户")
    @ApiImplicitParam(name = "user", value = "用户实体类", readOnly = true, dataType = "User", paramType = "body")
    public Map<String, Object> updateUser(@RequestBody @Valid User user) {
        Map<String, Object> map = new HashMap<>();
        // 通过try-catch 捕获service层抛出的异常
        // 通过try-catch 捕获service层抛出的异常
        // try {
        //     // 调用Service层的方法，返回Map对象
            userService.updateUser(user);
            map.put("code", 200);
            map.put("msg", "修改成功");
        // } catch (ParamsException p) {
        //     map.put("code", p.getCode());
        //     map.put("msg", p.getMsg());
        // } catch (Exception e) {
        //     map.put("code", 500);
        //     map.put("msg", "修改用户失败");
        // }
        return map;
    }

    @PutMapping("/user02")
    @ApiOperation(value = "修改用户")
    @ApiImplicitParam(name = "user", value = "用户实体类", readOnly = true, dataType = "User", paramType = "body")
    public Map<String, Object> updateUser02(@Valid User user) {
        Map<String, Object> map = new HashMap<>();
        // 通过try-catch 捕获service层抛出的异常
        // 通过try-catch 捕获service层抛出的异常
        // try {
        //     // 调用Service层的方法，返回Map对象
        // userService.updateUser(user);
        // map.put("code", 200);
        // map.put("msg", "修改成功");
        // } catch (ParamsException p) {
        //     map.put("code", p.getCode());
        //     map.put("msg", p.getMsg());
        // } catch (Exception e) {
        //     map.put("code", 500);
        //     map.put("msg", "修改用户失败");
        // }
        return map;
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/user/{userId}")
    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(name = "userId", value = "用户id", readOnly = true, paramType = "path")
    public Map<String, Object> deleteUserById(@PathVariable Integer userId) {
        Map<String, Object> map = new HashMap<>();
        // 通过try-catch 捕获service层抛出的异常
        // try {
            // 调用Service层的方法，返回Map对象
            userService.deleteUserById(userId);
            map.put("code", 200);
            map.put("msg", "删除成功");
        // } catch (ParamsException p) {
        //     map.put("code", p.getCode());
        //     map.put("msg", p.getMsg());
        // } catch (Exception e) {
        //     map.put("code", 500);
        //     map.put("msg", "删除用户失败");
        // }
        return map;
    }


    /**
     * 分页条件查询
     * @param userQuery
     * @return
     */
    @PostMapping("/userList")
    @ApiOperation(value = "分页查询用户")
    @ApiImplicitParam(name = "userQuery", value = "查询条件实体类", readOnly = true, dataType = "UserQuery", paramType = "body")
    public PageInfo<User> queryUserByParams(@RequestBody UserQuery userQuery) {
        return userService.queryUserByParams(userQuery);
    }
}
